package cat.joronya.supermanager.service;

import java.util.Hashtable;
import java.util.List;

import org.htmlcleaner.ContentNode;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.util.Log;


/**
 * Parse the matches of the day from ACB page.
 * http://www.acb.com/resulcla.php?codigo=LACB-56&jornada=10&resultados=1
 * 
 * @author pol
 */
public class DayMatchesParser 
{
	private static final String TAG = "supermanager";
	
	// team name mapping
	private Hashtable<String, String> teamNameToAcronim = new Hashtable<String, String>();
	
	// matches XPATH
	private static final String MATCHES_XPATH = "//table[@class='resultados']";
	
	public DayMatchesParser()
	{
		teamNameToAcronim.put("La Bruixa d'Or","BXA");
		teamNameToAcronim.put("FIATC Joventut","FIATC");
		teamNameToAcronim.put("FC Barcelona","FCB");
		teamNameToAcronim.put("Valencia Basket Club","VBC");
		teamNameToAcronim.put("Bilbao Basket","BB");
		teamNameToAcronim.put("Unicaja","UNI");
		teamNameToAcronim.put("Estudiantes","EST");
		teamNameToAcronim.put("Real Madrid","RM");
		teamNameToAcronim.put("Laboral Kutxa","LBO");
		teamNameToAcronim.put("Herbalife Gran Canaria","HGC");
		teamNameToAcronim.put("UCAM Murcia CB","UCAM");
		teamNameToAcronim.put("Baloncesto Fuenlabrada","FUE");
		teamNameToAcronim.put("Gipuzkoa Basket","GBC");
		teamNameToAcronim.put("CAI Zaragoza","CAI");
		teamNameToAcronim.put("CB Canarias", "CAN");
		teamNameToAcronim.put("Rio Natura Monbus","RNM");
		teamNameToAcronim.put("CB Valladolid","VAL");
		teamNameToAcronim.put("Cajasol","CAJ");
	}
	
	@SuppressWarnings("rawtypes")
	public Hashtable<String, String> parse(String html) throws Exception
	{
		Hashtable<String, String> matches = new Hashtable<String, String>();
		
		HtmlCleaner cleaner = new HtmlCleaner();
		TagNode node = cleaner.clean(html);
		
		// search the matches
		Object[] tables = node.evaluateXPath(MATCHES_XPATH);
		
		// some error and no match found
		if( tables.length == 0 )
			return null;
	
		TagNode table = (TagNode)tables[0];
		TagNode tbody = table.getChildTags()[0];
		
		// every tr has a match
		List trs = tbody.getChildren();
		for( int i=2; i<trs.size(); i++ )
		{
			TagNode tr = (TagNode)trs.get(i);
			List tds = tr.getChildren();
			
			// home team
			TagNode homeTeamTd = (TagNode)tds.get(0);
			List homeTeamTdChilds = homeTeamTd.getChildren();
			String homeTeam;
			if( homeTeamTdChilds.get(0) instanceof ContentNode )
			{
				homeTeam = ((ContentNode)homeTeamTdChilds.get(0)).toString();
				homeTeam = homeTeam.replace(" |", "");
			}
			else
			{
				TagNode aHomeTeam = (TagNode)homeTeamTdChilds.get(0);
				List anchorChilds = aHomeTeam.getChildren();
				homeTeam = ((ContentNode)anchorChilds.get(0)).toString();
			}
			homeTeam = teamNameToAcronim.get(homeTeam);
			
			// vs team
			TagNode vsTeamTd = (TagNode)tds.get(1);
			List vsTeamTdChilds = vsTeamTd.getChildren();
			String vsTeam;
			if( vsTeamTdChilds.get(0) instanceof ContentNode )
			{
				vsTeam = ((ContentNode)vsTeamTdChilds.get(0)).toString();
			}
			else
			{
				TagNode aVsTeam = (TagNode)vsTeamTdChilds.get(0);
				List anchorChilds = aVsTeam.getChildren();
				vsTeam = ((ContentNode)anchorChilds.get(0)).toString();
			}
			vsTeam = teamNameToAcronim.get(vsTeam);
			
			// store teams only if parsed
			if( homeTeam != null && vsTeam != null && 
				!"".equals(homeTeam) && !"".equals(vsTeam) )
			{
				Log.d(TAG,"DayMatchesParser.parse found match: "+homeTeam+" vs "+vsTeam);
				matches.put(homeTeam, vsTeam);
				matches.put(vsTeam, homeTeam);
			}
		}
		
		return matches;
	}
}
